package com.lai.note.util;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.widget.TextView;

import com.lai.note.activity.LoginActivity;
import com.lai.note.activity.MainActivity;
import com.lai.note.activity.PersonDetailsActivity;
import com.lai.note.dto.ResponseResult;
import com.lai.note.service.UserBaseService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class UserBaseUtil {

    private static Retrofit retrofit = null;
    private static UserBaseService userBaseService = null;
    public static boolean ISLOGINED=false;
    public static void judgeUserIsLogined(Context context){
        retrofit = RetrofitRequest.getInstance(context);
        userBaseService = retrofit.create(UserBaseService.class);
        Call<ResponseResult> call = userBaseService.isLogined();
        call.enqueue(new Callback<ResponseResult>() {
            @Override
            public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                ResponseResult res = response.body();
                if(res!=null && res.getStatus()==200){
                    ISLOGINED = true;
                }
            }

            @Override
            public void onFailure(Call<ResponseResult> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    public static void getCode(Context context,String phone){
        retrofit = RetrofitRequest.getInstance(context);
        userBaseService = retrofit.create(UserBaseService.class);
        Call<ResponseResult> call = userBaseService.getCode(phone);
        call.enqueue(new Callback<ResponseResult>() {
            @Override
            public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                ResponseResult res = response.body();
                System.out.println("res:-->"+res);
                if(res.getStatus()==200){
                    ToastUtil.display(context,"验证码为："+res.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResponseResult> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    public static void register(Context context,String phone,String code,String loginPwd,String confirmPwd){
        retrofit = RetrofitRequest.getInstance(context);
        userBaseService = retrofit.create(UserBaseService.class);
        // TODO: 2021/7/7 写一个工具类，获取用户经纬度
        Call<ResponseResult> call = userBaseService.fetchRegisterResult(phone,code, loginPwd,confirmPwd,175.3,25.2);
        call.enqueue(new Callback<ResponseResult>() {
            @Override
            public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                ResponseResult res = response.body();//返回的结果
                if(res!=null && res.getStatus()==200){
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.putExtra("phone",phone);
                    intent.putExtra("pwd",loginPwd);
                    context.startActivity(intent);
                    ToastUtil.display(context,"注册成功！\n登录后开启愉快的记录之旅吧！");
                }else if(res!=null && res.getStatus()==201){
                    ToastUtil.display(context,res.getMessage());
                }else{
                    ToastUtil.display(context,"卡bug了---注册失败-_-");
                }
            }

            @Override
            public void onFailure(Call<ResponseResult> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    public static void updateUserLoginPwdByCode(Context context, String phone, String code, String firstPwd, String confirmPwd, TextView tv){
        if(firstPwd==null){
            ToastUtil.display(context,"请输入密码！");
            return ;
        }else if(confirmPwd==null || !firstPwd.equals(confirmPwd)){
            ToastUtil.display(context,"两次输入的密码不一致！");
            return ;
        }
        retrofit = RetrofitRequest.getInstance(context);
        userBaseService = retrofit.create(UserBaseService.class);
        Call<ResponseResult> call = userBaseService.updateUserLoginPwdByCode(phone, code, confirmPwd);
        call.enqueue(new Callback<ResponseResult>() {
            @Override
            public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                ResponseResult res = response.body();
                if(res!=null && res.getStatus()==200){
                    ToastUtil.display(context,"修改密码成功！\n即将自动登录");
                    MyCountTimer myCountTimer = new MyCountTimer(4000,1000,tv,"倒计时开始") {
                        @Override
                        public void onFinish() {
                            ToastUtil.display(context,"倒计时结束了");
                            Intent intent = new Intent(context,MainActivity.class);
                            context.startActivity(intent);
                        }
                    };
                }
            }

            @Override
            public void onFailure(Call<ResponseResult> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    public static void updateUserInfo(Context context,String userDtoJson){
        retrofit = RetrofitRequest.getInstance(context);
        userBaseService = retrofit.create(UserBaseService.class);
        Call<ResponseResult> call = userBaseService.updateUser(userDtoJson);
        call.enqueue(new Callback<ResponseResult>() {
            @Override
            public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                ResponseResult res = response.body();
                if (res != null && res.getStatus() == 200) {
                    ToastUtil.display(context, "修改成功！");
                    // TODO: 2021/7/9 界面刷新
                    Intent intent = new Intent(context, PersonDetailsActivity.class);
                    context.startActivity(intent);
//                    finish();//关闭自己
//                    context.overridePendingTransition(0, 0);
                } else {
                    ToastUtil.display(context, "-_-出bug了喔，不知咋回事哩-_-");
                }
            }

            @Override
            public void onFailure(Call<ResponseResult> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }



}
